package com.yunkeji.common.enums;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;

import java.util.function.Function;

/**
 * 脱敏策略
 *
 * @author Yjoioooo
 * @version 3.6.0
 */
@AllArgsConstructor
public enum SensitiveStrategy {

    /** 身份证脱敏 */
    ID_CARD(s -> DesensitizedUtil.idCardNum(s, 3, 4)),

    /** 手机号脱敏 */
    PHONE(DesensitizedUtil::mobilePhone),

    /** 地址脱敏 */
    ADDRESS(s -> DesensitizedUtil.address(s, 8)),

    /** 邮箱脱敏 */
    EMAIL(DesensitizedUtil::email),
    /** 隐藏全部 */
    HIDE_ALL(S -> "*****"),

    /** 隐藏不分 */
    HIDE_PART(
            s -> {
                if (StrUtil.isEmpty(s)) return s;
                if (s.length() <= 2) {
                    return "**";
                } else {
                    String[] split = s.split("");
                    return split[0] + "****" + split[split.length - 1];
                }
            }),
    /** 银行卡 */
    BANK_CARD(DesensitizedUtil::bankCard);

    // 可自行添加其他脱敏策略

    private final Function<String, String> desensitizer;

    public Function<String, String> desensitizer() {
        return desensitizer;
    }
}
